//输入一个整数数组，实现一个函数来调整该数组中数字的顺序，使得所有奇数在数组的前半部分，所有偶数在数组的后半部分。 
//
// 
//
// 示例： 
//
// 
//输入：nums = [1,2,3,4]
//输出：[1,3,2,4] 
//注：[3,1,2,4] 也是正确的答案之一。 
//
// 
//
// 提示： 
//
// 
// 0 <= nums.length <= 50000 
// 0 <= nums[i] <= 10000 
// 
// Related Topics 数组 双指针 排序 👍 226 👎 0

package leetcode.editor.offer;

// 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
// https://leetcode.cn/problems/diao-zheng-shu-zu-shun-xu-shi-qi-shu-wei-yu-ou-shu-qian-mian-lcof/
class DiaoZhengShuZuShunXuShiQiShuWeiYuOuShuQianMianLcof {
    public static void main(String[] args) {
        Solution solution = new DiaoZhengShuZuShunXuShiQiShuWeiYuOuShuQianMianLcof().new Solution();
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        /*public int[] exchange(int[] nums) {
            int pre = 0;
            int after = nums.length - 1;
            while (pre <= after) {
                if (nums[pre] % 2 == 1) {
                    pre++;
                } else if (nums[after] % 2 == 0) {
                    after--;
                } else if (nums[after] % 2 == 1 && nums[pre] % 2 == 0) {
                    int temp = nums[after];
                    nums[after] = nums[pre];
                    nums[pre] = temp;
                }
            }
            return nums;
        }*/

        // 双指针，冒泡排序思想
        public int[] exchange(int[] nums) {
            int pre = 0, cur = nums.length - 1;
            while (pre <= cur) {
                while (pre < cur && nums[pre] % 2 == 1) pre++;
                while (pre < cur && nums[cur] % 2 == 0) cur--;
                swap(nums, pre, cur);
            }

            return nums;
        }

        void swap(int[] nums, int i, int j) {
            int t = nums[i];
            nums[i] = nums[j];
            nums[j] = t;
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}
